package com.leyou.item.service;

import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SpecificationService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    //查询 规格组
    public List<SpecGroup> querySpecGroups(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        List<SpecGroup> list = this.specGroupMapper.select(specGroup);
        return list;
    }

    //查询 规格参数
    public List<SpecParam> querySpecParam(Long gid, Long cid, Boolean searching, Boolean generic) {
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
        specParam.setGeneric(generic);
        List<SpecParam> list = this.specParamMapper.select(specParam);
        return list;
    }

    //新增 规格分组
    public int addSpecGroup(String name, Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        specGroup.setName(name);
        int i = this.specGroupMapper.insertSelective(specGroup);
        return i;
    }

    //修改 规格分组
    public int editSpecGroup(Long id, String name, Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setName(name);
        specGroup.setCid(cid);
        specGroup.setId(id);
        int i = this.specGroupMapper.updateByPrimaryKeySelective(specGroup);
        return i;
    }
    //删除 规格分组
    @Transactional  //添加事务管理
    public int delSpecGroup(Long id) {
        int i = this.specGroupMapper.deleteByPrimaryKey(id);
        //若直接删除规格分组，则其下面的规格参数也应一并删除，规格参数是根据gid进行删除
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(id);
        this.specParamMapper.delete(specParam);
        return i;
    }

    //新增 规格参数
    public int addSpecParam(Long cid, Long groupId, String name, Boolean numeric, String unit, Boolean generic, Boolean searching, String segments) {
        SpecParam specParam = new SpecParam();
        specParam.setCid(cid);
        specParam.setGroupId(groupId);
        specParam.setName(name);
        specParam.setNumeric(numeric);
        specParam.setUnit(unit);
        specParam.setGeneric(generic);
        specParam.setSearching(searching);
        specParam.setSegments(segments);
        int i = this.specParamMapper.insertSelective(specParam);
        return i;
    }
    //修改 规格参数
    public int editSpecParam(SpecParam specParam) {
        int i = this.specParamMapper.updateByPrimaryKeySelective(specParam);
        return i;
    }
    //删除  规格参数
    public int delSpecParam(Long id) {
        int i = this.specParamMapper.deleteByPrimaryKey(id);
        return i;
    }

    //查询规格参数组和组内信息
    public List<SpecGroup> querySpecsByCid(Long cid) {
        List<SpecGroup> specGroups = querySpecGroups(cid);
        //遍历规格参数组，根据组id查询对应的规格参数信息，并添加到specGroup对象中
        specGroups.forEach(spec -> {
            //查询组内参数
            spec.setSpecParams(querySpecParam(spec.getId(),null,null,null));
        });
        return specGroups;
    }
}
